<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdjustController extends Controller
{
    //===============显示列表=============
    public function getIndex(Request $request){

     if($request->input('name')){
            //搜索分页
            $res = DB::table('adjust')
            ->join('person','person.id','=','adjust.Personid')
            ->join('department','department.id','=','person.DepartmentID')
            ->where('PersonName','like','%'.$request->input('name').'%')
            ->select('adjust.*','person.PersonName','department.DepartmentName')
            ->paginate(1);
           
           $res1 = DB::table('department')
           ->join('adjust','adjust.NewDepartmentid','=','department.id')
           ->select('DepartmentName')
           ->paginate(1);
           
            
        }else{
            //所有分页      
            $res = DB::table('adjust')
            ->join('person','person.id','=','adjust.Personid')
            ->join('department','department.id','=','person.DepartmentID')
            ->select('adjust.*','person.PersonName','department.DepartmentName')
            ->paginate(1);
             $res1 = DB::table('department')
               ->join('adjust','adjust.NewDepartmentid','=','department.id')
               ->select('DepartmentName')
               ->paginate(1);
               //dd($res1);
            }

            /* $newname = $res[0]['NewDepartmentid'];
            dd($newname); */ 
                  
        $info = $request -> all();    
        return view('Personnel/Adjust/index',['res'=>$res,'res1'=>$res1,'info'=>$info]);
}

    //=============添加========
    public function getAdd(){
         $name = session('username');
        //dd($name);
         $res = DB::table('department')
            ->join('person','person.DepartmentID','=','department.id')
           ->where('PersonName','=',$name)
           ->select('department.id','DepartmentName')
            ->get();
            //dd($res);
             $res1 = DB::table('department')
               ->select('id','DepartmentName')
               ->get();
               //dd($res1);

    	//解析模板
    	return view('Personnel/Adjust/add',['res'=>$res,'res1'=>$res1]);
    }
    //======执行添加====
    public function postInsert(Request $request){
        $name = session('username');
        $ids = DB::table('person')->where('PersonName','=',$name)->select('id')->first();
        //dd($info);
        //dd($name);
         $res3 = DB::table('department')
            ->join('person','person.DepartmentID','=','department.id')
           ->where('PersonName','=',$name)
           ->select('department.id','DepartmentName')
            ->first();
            //dd($res3);
    	//接收数据
    	$date = $request ->all();
         
        $date['Personid']=$ids['id'];
        $date['OldDepartmentid'] = $res3['id'];
        //dd($date);
        //随机token
        $date['_token']=str_random(50);
    	//dd($date); 
        //添加sql语句
        $res = DB::table('Adjust')->insert($date);
       if($res){
            //成功跳转至列表页
            return redirect('/Personnel/Adjust/index')->with('success','职位调整申请添加成功');
        }else{
           return back()->with('error','职位调整申请添加失败');
        }                                                                                     
    }
    //================编辑=======
    public function getEdit($id){
         //dd($id);
        

             $info = DB::table('adjust')
            ->join('person','person.id','=','adjust.Personid')
            ->join('department','department.id','=','person.DepartmentID')
            ->where('adjust.id',$id)
            ->select('adjust.*','person.id','person.PersonName','department.DepartmentName')
            ->get();
            $info1 = DB::table('department')
               ->join('adjust','adjust.NewDepartmentid','=','department.id')       
               ->select('DepartmentName')
               ->get();
               $name = session('id');
               if($name!=$info[0]['id']){
                return back()->with('error','不是本人');
               }
        //dd($info);
        //解析模板显示编辑的页面
            return view('Personnel/Adjust.edit',['info'=>$info,'info1'=>$info1,'id'=>$id]);
    }
    //================执行编辑
    public function postUpdate(Request $request){


        $info = $request ->all();
        $id = $info['id'];
        DB::table('Adjust')->where('id',$id)->update($info);
        return redirect('Personnel/Adjust/index')->with('修改成功');
    }   
   
    //=============================删除===========================
    public function getDel($id){
        //执行删除
        //dd($id);
        DB::table('Adjust')->where('id',$id)->delete();
        //解析模板显示列表的页面
        return redirect('Personnel/Adjust/index')->with('删除成功');
    }



}
?>